package com.hqully.labmanage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqully.labmanage.common.enums.ResultEnum;
import com.hqully.labmanage.common.exception.DAOException;
import com.hqully.labmanage.common.exception.ResultException;
import com.hqully.labmanage.domain.dto.LabManagerDTO;
import com.hqully.labmanage.domain.dto.PageDTO;
import com.hqully.labmanage.domain.entity.LabManager;
import com.hqully.labmanage.mapper.LabManagerMapper;
import com.hqully.labmanage.service.LabManagerService;
import com.hqully.labmanage.service.impl.constant.MapperConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 针对数据库表[labManager]的操作Service
 * </p>
 *
 * @entity {@link LabManager}
 * @date 2022-10-08 10:05:23 <br>
 * @author hqully <br>
 * @version 1.0
 */
@Slf4j
@Service
public class LabManagerServiceImpl implements LabManagerService {


    @Resource
    private LabManagerMapper labManagerMapper;

    @Override
    public PageInfo<LabManagerDTO> pageLabManagers(PageDTO page, LabManagerDTO labManagerDTO) {
        try {
            PageHelper.startPage(page);
            List<LabManagerDTO> labManagerList = labManagerMapper.listLabManagers(labManagerDTO);
            return new PageInfo<>(labManagerList);
        } catch (Exception e) {
            log.error("PAGE [labManager] FAIL\nINPUT OBJECT: {}\nREASON: {}", labManagerDTO, e.toString());
            throw new ResultException(ResultEnum.ERROR, "查询实验室管理员分页列表失败");
        }
    }

    @Override
    public List<LabManagerDTO> listLabManagers(LabManagerDTO labManagerDTO) {
        try {
            return labManagerMapper.listLabManagers(labManagerDTO);
        } catch (Exception e) {
            log.error("LIST [labManager] FAIL\nINPUT OBJECT: {}\nREASON: {}", labManagerDTO, e.toString());
            throw new ResultException(ResultEnum.ERROR, "查询实验室管理员列表失败");
        }
    }

    @Override
    public int addLabManager(LabManagerDTO labManagerDTO) {
        LabManager labManager = new LabManager();
        BeanUtils.copyProperties(labManagerDTO, labManager);

        int status;
        try {
            status = labManagerMapper.addLabManager(labManager);
        } catch (DuplicateKeyException e) {
            log.warn("ADD [labManager] FAIL\nINPUT OBJECT: {}\nREASON: {}", labManagerDTO, e.toString());
            throw new ResultException(ResultEnum.DUPLICATE_KEY_CONFLICT, "添加实验室管理员失败\n原因: 实验室管理员id已存在");
        } catch (Exception e) {
            log.error("ADD [labManager] FAIL\nINPUT OBJECT: {}\nREASON: {}", labManagerDTO, e.toString());
            throw new ResultException(ResultEnum.ERROR, "添加实验室管理员失败");
        }
        return status;
    }

    @Override
    public int updateLabManager(LabManagerDTO labManagerDTO) {
        LabManager labManager = new LabManager();
        BeanUtils.copyProperties(labManagerDTO, labManager);

        int status;
        try {
            status = labManagerMapper.updateLabManager(labManager);
            if (status == MapperConst.OBJECT_NULL) {
                throw new DAOException("this [labManager] record not exist");
            }
        }catch (DAOException e){
            log.warn("UPDATE [labManager] FAIL\nINPUT OBJECT: {}\nREASON: {}", labManagerDTO, e.toString());
            throw new ResultException(ResultEnum.RESULT_NULL, "更新实验室管理员失败\n原因: 实验室管理员不存在");
        }
        catch (Exception e) {
            log.error("UPDATE [labManager] FAIL\nINPUT OBJECT: {}\nREASON: {}", labManagerDTO, e.toString());
            throw new ResultException(ResultEnum.ERROR, "更新实验室管理员失败");
        }
        return status;
    }

    @Override
    public int deleteLabManagerById(String id) {
        int status;
        try {
            status = labManagerMapper.deleteLabManagerById(id);
            if (status == MapperConst.OBJECT_NULL) {
                throw new DAOException("this [labManager] record not exist");
            }
        } catch (DAOException e) {
            log.warn("DELETE [labManager] FAIL\nINPUT OBJECT: {}\nREASON: {}", id, e.toString());
            throw new ResultException(ResultEnum.RESULT_NULL, "删除实验室管理员失败\n原因: 实验室管理员不存在");
        } catch (Exception e) {
            log.error("DELETE [labManager] FAIL\nINPUT OBJECT: {}\nREASON: {}", id, e.toString());
            throw new ResultException(ResultEnum.ERROR, "删除实验室管理员失败");
        }
        return status;
    }


}



